package top.coolrainly.module.pb.controller.admin.theme;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import top.coolrainly.framework.common.pojo.CommonResult;
import top.coolrainly.framework.common.util.object.BeanUtils;
import static top.coolrainly.framework.common.pojo.CommonResult.success;

import top.coolrainly.module.pb.controller.admin.theme.vo.*;
import top.coolrainly.module.pb.convert.theme.ThemeConvert;
import top.coolrainly.module.pb.dal.dataobject.theme.ThemeDO;
import top.coolrainly.module.pb.service.theme.ThemeService;

@Tag(name = "管理后台 - 党建 主题")
@RestController
@RequestMapping("/pb/theme")
@Validated
public class ThemeController {

    @Resource
    private ThemeService themeService;

    @PostMapping("/create")
    @Operation(summary = "创建党建 主题")
    @PreAuthorize("@ss.hasPermission('pb:theme:create')")
    public CommonResult<Long> createTheme(@Valid @RequestBody ThemeSaveReqVO createReqVO) {
        return success(themeService.createTheme(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新党建 主题")
    @PreAuthorize("@ss.hasPermission('pb:theme:update')")
    public CommonResult<Boolean> updateTheme(@Valid @RequestBody ThemeSaveReqVO updateReqVO) {
        themeService.updateTheme(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除党建 主题")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pb:theme:delete')")
    public CommonResult<Boolean> deleteTheme(@RequestParam("id") Long id) {
        themeService.deleteTheme(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得党建 主题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pb:theme:query')")
    public CommonResult<ThemeRespVO> getTheme(@RequestParam("id") Long id) {
        ThemeDO theme = themeService.getTheme(id);
        return success(ThemeConvert.INSTANCE.convert(theme));
    }

    @GetMapping("/list")
    @Operation(summary = "获得所有主题")
    @PreAuthorize("@ss.hasPermission('pb:theme:query')")
    public CommonResult<List<ThemeRespVO>> List() {
        return success(ThemeConvert.INSTANCE.convertList01(themeService.getThemeList()));
    }

}